#ifndef __SINGAPOREBUSSTOPSSVIEW__
#define __SINGAPOREBUSSTOPSSVIEW__

// [[[ begin generated region: do not modify [Generated Includes]
#include <aknview.h>
// ]]] end generated region [Generated Includes]

#include "CSingaporeBusGenericView.h"
#include "googlepositioning.h"
#include "positionrequest.h"
#include "SingaporeBusStructures.h"
#include "AsyncFileLoader.h"
#include "IteratorControl.h"
#include <SingaporeBus.RSG>
#include <aknnavide.h>
#include <euserhl.h>
#include "CharUtils.h"
#include "SingaporeBusAppUi.h"
#include "DrawUtils.h"
#include "SingaporeBusDocument.h"



class CSingaporeBusStopsView : public CSingaporeBusGenericView<const TStation*, CSingaporeBusStopsView>
	{
	
public:

	virtual TBool FilterItem(const TStation* aItem, const TDesC8& aSearchText) { return CSingaporeBusGenericHandler::FilterItem(aItem, aSearchText); }
	virtual TReal32 OrderItem(const TStation* aItem)
		{
		return CSingaporeBusGenericHandler::OrderItem(aItem);
		}
	virtual TBool ListBoxObserver(const TStation* aItem, CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType)  { return CSingaporeBusGenericHandler::ListBoxObserver(aItem, aListBox, aEventType); }
	virtual TBool HandleCommandL( const TStation* aItem, TInt aCommand ) 
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);
		this->iContainer->iIteratorControl->DrawDeferred();
		TBool ret = CSingaporeBusGenericHandler::HandleCommandL(aItem, aCommand);
		return ret;
		}
		
	virtual CEikListBox* CreateListBoxL()
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);
		
		//return ConstructListBoxL( new CSingaporeBusListBox2 );
		//return ConstructListBoxL( new CAknDoubleGraphicStyleListBox );
		
		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
		TInt minHeight = doc->iSingaporeBusDrawItemsSG->PrimaryFont()->FontMaxHeight() + 3*doc->iSingaporeBusDrawItemsSG->SecondaryFont()->FontMaxHeight();
		if(ClientRect().Height() / minHeight < 4)
			minHeight -= doc->iSingaporeBusDrawItemsSG->SecondaryFont()->FontMaxHeight();
		return ConstructListBoxL(
				new (ELeave) CSingaporeBusListBox,
				minHeight + (ClientRect().Height() % minHeight)/(ClientRect().Height() / minHeight)
				);
		}
	virtual void FormatItem(TDes& aBuf, const TStation* aItem)
	{
	aBuf.Format(_L("5\t%S \x2022 %S\t%S"), narrowToWideLC(aItem->iAddress2), narrowToWideLC(aItem->iAddress), narrowToWideLC(aItem->iStationNumber));
	CleanupStack::PopAndDestroy(3);

	
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	if(doc->iPositioningStatus == CSingaporeBusDocument::PS_FOUND)
		{
		TReal32 distance;
		doc->iPosition.Distance(aItem->iCoord, distance);
		aBuf.AppendFormat(_L(" \x21b3%.0fm"), (double)distance);
		}
	
	/*const CFont* refFont = ((CEikFormattedCellListBox*)this->iContainer->iIteratorControl->iListBox.Get())->ItemDrawer()->ColumnData()->SubCellFont(1);
	TFontSpec spec = refFont->FontSpecInTwips();
	aBuf.AppendFormat(_L(" |%d|"), spec.iHeight);
	aBuf.AppendFormat(_L(" |%Ld|"), 
			((CSingaporeBusListBox2*)this->iContainer->iIteratorControl->iListBox.Get())->iMinFrameDelay
	);*/
	
	aBuf.Append('\t');

	TBool favoriteStation = EFalse;
	for(TInt i = 0 ; i < doc->iFavorites.Count() && !favoriteStation; ++i)
		favoriteStation = ((doc->iFavorites[i])->Find(aItem->iStationNumber) != NULL);
	if(favoriteStation)
		{
		if(doc->iOriginStation == aItem)
			aBuf.Append('3');
		else if(doc->iDestinationStation == aItem)
			aBuf.Append('4');
		else
			aBuf.Append('0');
		}
	else
		{
		if(doc->iOriginStation == aItem)
			aBuf.Append('1');
		else if(doc->iDestinationStation == aItem)
			aBuf.Append('2');
		}
	}
	
	void DrawItem(CSingaporeBusListBoxItemDrawer* aDrawer, const TStation* aItem,
			TInt aItemIndex, const TRect& aActualItemRect,
			TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed, 
			TBool aItemIsSelected)
		{
		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();

		if(aItem->iStationType == TStation::ELDNormal)
			doc->iSingaporeBusDrawItemsLD->DrawBusStopItemL(aDrawer, aItem, aItemIndex, aActualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, aItemIsSelected);
		else
			doc->iSingaporeBusDrawItemsSG->DrawBusStopItemL(aDrawer, aItem, aItemIndex, aActualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed, aItemIsSelected);	
		
		}

	
	void ConstructL()
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);
		CSingaporeBusGenericView<const TStation*, CSingaporeBusStopsView>::ConstructL( R_SINGAPORE_BUS_STOPS_VIEW,  R_SINGAPORE_BUS_STOPS_MARK_MENU, R_SINGAPORE_BUS_STOPS_ICONS, ESingaporeBusStopsViewId );
		}
						
	void NotifyFileLoaded(const CAsyncFileLoader* aFile)
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);
		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
		if(aFile == doc->iStationsFileLoader)
			InitializeList();
		else if(aFile == doc->iServicesFileLoader)
			{
			// we draw the list of services
			if(this->iContainer)
				this->iContainer->iIteratorControl->DrawDeferred();
			}
		}
	
	void Breakpoint()
		{
		TInt i = 0;
		++i;
		}
	
		
protected:
	void DoActivateL(
		const TVwsViewId& aPrevViewId,
		TUid aCustomMessageId,
		const TDesC8& aCustomMessage )
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);
		CSingaporeBusGenericView<const TStation*, CSingaporeBusStopsView>::DoActivateL(aPrevViewId, aCustomMessageId, aCustomMessage );
		RestoreViewStateL(aCustomMessage);
		}
	
	void DoDeactivate()
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);
		CSingaporeBusGenericView<const TStation*, CSingaporeBusStopsView>::DoDeactivate();
		}
	
	void InitializeList()
		{
		TFunTrace funTrace(__PRETTY_FUNCTION__);
		CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
		this->SetUnderlyingL(
				new (ELeave) CArrayPtrIterator<const TStation*>(&doc->iStations[0], doc->iStations.Count())
				);
		if(this->iContainer)
			this->iContainer->iIteratorControl->DrawDeferred();
		}

	
	};

#endif // __SINGAPOREBUSSTOPSSVIEW__
